﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CefSharp.WinForms;

namespace WindowsForms_CEF
{
    public partial class CEFForm : Form
    {
        public CEFForm()
        {
            InitializeComponent();

            var mainAddress = "http://www.baidu.com";
            InitCEF(mainAddress);
        }

        public ChromiumWebBrowser CEFControl;
        private void InitCEF(string mainAddress)
        {

            CefSharp.Cef.Initialize();
            CEFControl = new ChromiumWebBrowser(mainAddress);
            CEFControl.Dock = DockStyle.Fill;
            this.CEFPanel.Controls.Add(CEFControl);

            CEFControl.AddressChanged += wbControl_AddressChanged;
            CEFControl.FrameLoadStart += wbControl_FrameLoadStart;
            CEFControl.Load("http://www.oschina.net");
        }

        void wbControl_FrameLoadStart(object sender, CefSharp.FrameLoadStartEventArgs e)
        {
            var js = "Frame Load Start for '+" + e.Url + "";
            //showMessage(js);
        }

        void wbControl_AddressChanged(object sender, CefSharp.AddressChangedEventArgs e)
        {
            var js = "Address Changed to '+" + e.Address + ")";
            //showMessage(js);
        }
        public void showMessage(string jsScript)
        {
            MessageBox.Show(jsScript);
        }
    }
}
